package Main;

import url.DBUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RegisterDialog extends JFrame {
    public RegisterDialog() {

        Font f = new Font("宋体", Font.PLAIN, 25);
        JPanel jPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 20, 10);

        JLabel j1 = new JLabel("账  号");
        j1.setFont(f);
        gbc.gridx = 0;
        gbc.gridy = 0;
        jPanel.add(j1, gbc);
        JTextField jText0 = new JTextField(15);
        jText0.setFont(f);
        gbc.gridx = 1;
        jPanel.add(jText0, gbc);

        JLabel j2 = new JLabel("密  码");
        j2.setFont(f);
        gbc.gridx = 0;
        gbc.gridy = 1;
        jPanel.add(j2, gbc);
        JPasswordField jText1 = new JPasswordField(15);
        jText1.setFont(f);
        gbc.gridx = 1;
        jPanel.add(jText1, gbc);

        JButton jb0 = new JButton("确定");
        jb0.setFont(f);
        gbc.gridx = 0;
        gbc.gridy = 10;
        gbc.gridwidth = 2; // 设置按钮占据两列
        gbc.anchor = GridBagConstraints.CENTER; // 居中对齐
        jPanel.add(jb0, gbc);
        ;

        jPanel.setOpaque(false);
        this.add(jPanel, BorderLayout.CENTER); // Remove unnecessary semicolon
        background();

        // 设置字体大小
        setFont(new Font("宋体", Font.PLAIN, 25));
        // 窗口大小
        setSize(700, 700);
        // 窗口可视化
        setVisible(true);
        // 窗口标题
        setTitle("场地管理系统");
        // 窗口初始化位置
        setLocationRelativeTo(null);
        // 窗口大小不可改变
        setResizable(false);
        // 窗口的关闭事件
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        jb0.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DBUtil.initst();
                String text = jText0.getText();
                char[] passwordChars = jText1.getPassword();
                String password = new String(passwordChars);
                String sql = "insert into account values('" + text + "','" + password + "')";
                DBUtil.uodate(sql);
                DBUtil.closeDB();
                dispose(); // 关闭当前窗口
            }
        });


    }
    public void background() {
        ((JPanel) this.getContentPane()).setOpaque(false);
        ImageIcon img = new ImageIcon("src/img/1.jpg"); //添加图片
        JLabel background = new JLabel(img);
        this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
        background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
    }

}
